import Emitter from 'events'
import { ModelEvent } from '.'

export default class SelectionModel extends Emitter {

  index = 0
  length = 0

  stack = {index:-1,length:-1}

  constructor() {
    super()
  }
  _notify() {
    this.emit(ModelEvent.SELECTION_CHANGE)
  }

  update(index:number,length:number){
    if(index === this.index && length === this.length) return
    this.index = index
    this.length = length
    this._notify()
  }
  sync(index:number,length:number){
    if(index === this.index && length === this.length) return
    this.index = index
    this.length = length
  }
  push(){
    this.stack.index = this.index
    this.stack.length = this.length
  }
  pop(){
    this.index = this.stack.index
    this.length = this.stack.length
  }

  get endIndex() {
    return this.index + this.length
  }

}